package com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.news.model.Comment;
import com.news.model.News;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {

    private Context context;
    private List<Comment> commentList;

    public static class CommentViewHolder extends RecyclerView.ViewHolder {
        public TextView userNameTextView;
        public TextView commentText;
        public TextView commentTime;

        public CommentViewHolder(View v) {
            super(v);
            userNameTextView = v.findViewById(R.id.comment_user);
            commentText = v.findViewById(R.id.comment_text);
            commentTime = v.findViewById(R.id.comment_time);
        }
    }

    public CommentAdapter(Context context, List<Comment> commentList) {
        this.context = context;
        this.commentList = commentList;
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_item, parent, false);
        return new CommentViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        Comment comment = commentList.get(position);
        Log.i("评论数据",comment.toString());
        holder.userNameTextView.setText(String.valueOf(comment.getUserName()));
        holder.commentText.setText(comment.getContent());

        // 将长整型时间戳转换为可读的日期时间字符串
        long publishedAt = comment.getCreatedAt();
        String formattedDate = formatDate(publishedAt);
        holder.commentTime.setText(formattedDate);
    }

    @Override
    public int getItemCount() {
        return commentList.size();
    }

    private String formatDate(long time) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdf.format(date);
    }
}